package com.imooc.util;

import com.a3test.component.idworker.IdWorkerConfigBean;
import com.a3test.component.idworker.Snowflake;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.generator.BeforeExecutionGenerator;
import org.hibernate.generator.EventType;

import java.util.EnumSet;

/**
 * 主键生成策略 雪花算法
 * Description:
 * Author: gzj
 * Date: 2024/10/25 10:51
 */
public class IdUtil implements BeforeExecutionGenerator {

    private static final Snowflake snowflake = new Snowflake(new IdWorkerConfigBean());


    @Override
    public Object generate(SharedSessionContractImplementor session, Object owner, Object currentValue, EventType eventType) {
        return snowflake.nextId();
    }

    @Override
    public EnumSet<EventType> getEventTypes() {
        return EnumSet.of(EventType.INSERT);
    }
}
